<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use app\admin\model\AuthroleModel;
use think\Controller;
use think\Config;
use think\Loader;
use think\Db;

class AuthroleController extends BaseController
{
    //主页
    public function index(){
        if (request()->isAjax()){
            $page=input("param.page");
            $limit=input("param.limit",100);
            $keyword=input("param.keyword");
            $where=[];
            $where="role_name like '%{$keyword}%'";
            $sum=($page-1)*$limit;
            $data=Db::name("auth_role")
                ->where($where)
                ->order("role_id ASC")
                ->limit("$sum,$limit")
                ->select();
            $num=Db::name("auth_role")
                ->where($where)
                ->order("role_id ASC")
                ->select();
            $result["code"]=0;
            $result["msg"]="";
            $result["count"]=count($num);
            $result["data"]=$data;
            $this->assign("is_role",is_role());
            return json($result);
        }
        $this->assign("is_role",is_role());
        return $this->fetch("index");
    }

//    角色的添加
    public function add(){
        if (request()->isPost()){
            $list=request()->post();
            $findname=Db::name("auth_role")->where("role_name","=",$list["role_name"])->find();
            if (!empty($findname)){
                return json(["code"=>0,"msg"=>"该角色名称已存在"]);
            }
            $list["modified"]=date("Y-m-d H:i:s");
            $result=Db::name("auth_role")->insert($list);
            if ($result){
                return json(["code"=>1,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>0,"msg"=>"添加失败"]);
            }
        }
        return $this->fetch("add");
    }

    /*
     * 编辑
     */
    public function edit(){
        $role_id=input("param.role_id");
        if (request()->isPost()){
            $list=request()->post();
            $result=Db::name("auth_role")->where("role_id","=",$list["role_id"])->update($list);
            if ($result){
                return json(["code"=>1,"msg"=>"编辑成功"]);
            }else{
                return json(["code"=>0,"msg"=>"编辑失败"]);
            }
        }
        $where["role_id"]=["=",$role_id];
        $find=Db::name("auth_role")->where($where)->find();
        $this->assign("find",$find);
        return $this->fetch("edit");
    }
    
    /*
     * 删除
     */
    public function del(){
        $role_id=input("param.auth_role");
        $result=Db::name("auth_role")->where(["role_id"=>$role_id])->delete();
        writelog("删除角色成功");
        if ($result){
            return json(["code"=>1,"msg"=>"删除成功"]);
        }else{
            return json(["code"=>0,"msg"=>"删除失败"]);
        }
    }


    /**
     * [分配权限]
     */
    public function give(){

        $param = input('param.');

        $node = new AuthroleModel();
        //获取现在的权限
        if('get' == $param['type']){
            $nodeStr = $node->getNodeInfo($param['id']);
            return json(["code"=>1,"data"=>$nodeStr]);
        }
        //分配新权限
        if('give' == $param['type']){
            // 启动事务
            Db::startTrans();
            try{
                $doparam["menu_id"]=$param['rule'];
                $data=Db::name("auth_role")->where(["role_id"=>$param["id"]])->update($doparam);
                // 提交事务
                Db::commit();
                return json(["code"=>1,"msg"=>"分配权限成功"]);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(["code"=>0,"msg"=>"分配权限失败"]);
            }
        }

    }




}